import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home.vue'
import HomeMain from '@/views/Home/Main.vue'




import Login from '@/views/login/Login.vue'
import register from '@/views/register/register.vue'
import CreateArticle from '@/views/creationArticle/CreateArticle.vue'
import profile from '@/views/profile/profile.vue'
import article from '@/views/articleDetails/articleDetails.vue'
import forgetPassword from '@/views/forgetPassword/forgetPassword.vue'
import searchResults from '@/views/searchResults/searchResults.vue'
import classify from '@/views/classify/classify.vue'
import classifyDetail2 from '@/views/classify/classifyDetail2.vue'
import VIP from '@/views/VIP/VIP.vue'
import {getToken} from '@/request/token'
import {getUserInfo} from '@/api/user'
Vue.use(VueRouter)

const routes = [
  {
    path: '',
    redirect: '/home'

  },
  {
    path: '/',
    name: 'home',
    component: Home,
    children: [
      {
        path: 'home',
        component:HomeMain
      },
      {
        path: 'classify',
        component:classify
        

      },
      {
        path: 'searchResults',
        name:'searchResults',
        component: searchResults
      },
      {
        path: 'profile',
        
        component: profile

      },
      {
        path: 'article',
        component: article,
        name:"article"
      },{
        path: 'VIP',
        component: VIP,
      },
      {
        path: 'classifyDetail2',
        name:'classifyDetail2',
        component:classifyDetail2
      }
      
    ]
    
  },
  {
    path: '/create-article',
    name: 'createArticle',
    component: CreateArticle,
  },
  

  {
    path: '/login',
    name: 'login',
    component: Login
  },

 
  {
    path: '/register',
    name: 'register',
    component: register
  },
  {
    path: '/forgetPassword',
    name: 'forgetPassword',
    component: forgetPassword,

  },




]

const router = new VueRouter({
  mode: 'hash',
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.path==='/create-article'||to.path==='/VIP'){
    const token=getToken()
    if(token===null||token===''){
    return next('/login')
    }

    getUserInfo(token).then(res=>{
      if(!res.success){
        return next('/login')
      }else{
        return next()
      
      }
      
    })

  }
  next()
})

export default router
